﻿using System;
using Sirenix.OdinInspector;
using Sirenix.OdinInspector.Editor;
using Sirenix.Utilities;
using Sirenix.Utilities.Editor;
using UnityEditor;
using UnityEngine;

namespace YooAsset.Editor.AssetBundleOdinEditor
{
    public class AssetBundleBuilderOdinEditor : OdinEditorWindow
    {
        [FolderPath(AbsolutePath = true)] public string BuildOutPut;

        [Header("是否强制更新")] public bool ForceRebuild;

        [Header("资源版本号")] public int BuildVersion;

        [Header("压缩设置")] public ECompressOption CompressOption = ECompressOption.LZ4;

        [Header("加密方式")] public IEncryptionServices EncryptionServices;

        [Header("增量更新")] public bool AppendExtension;

        [Header("打包标识")] public string BuildTags;

        [MenuItem("YooAsset/OdinAssetBundleBuild")]
        private static void OpenWindow()
        {
            var window = GetWindow<AssetBundleBuilderOdinEditor>();
            window.position = GUIHelper.GetEditorWindowRect().AlignCenter(700, 700);
        }
        
        [Button(ButtonSizes.Large, Name = "构建")]
        [Title("打包之前请设置版本号")]
        private void ExecuteBuild()
        {
            string defaultOutputRoot = AssetBundleBuilderHelper.GetDefaultOutputRoot();
            BuildParameters buildParameters = new BuildParameters();
            buildParameters.VerifyBuildingResult = true;
            buildParameters.OutputRoot = defaultOutputRoot;
            buildParameters.BuildTarget = EditorUserBuildSettings.activeBuildTarget;
            buildParameters.BuildVersion = BuildVersion;
            buildParameters.CompressOption = CompressOption;
            buildParameters.AppendFileExtension = AppendExtension;
            buildParameters.EncryptionServices = EncryptionServices;
            buildParameters.ForceRebuild = ForceRebuild;
            buildParameters.BuildinTags = BuildTags;
            AssetBundleBuilder builder = new AssetBundleBuilder();
            builder.Run(buildParameters);
        }
    }
}